Imports System.IO
Imports GeoFramework.Gps.Emulators
Imports GeoFramework.Gps.Nmea

Public Class LoggingGpsDataForm

#Region " Recording Data to a Stream "

    Private Sub StartButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles StartButton.Click
        ' Start receiving GPS Data
        RecordingInterpreter.Start()

        ' Enable Pause and Stop buttons
        StartButton.Enabled = False
        PauseButton.Enabled = True
        StopButton.Enabled = True
    End Sub

    Private Sub OutputFileBrowseButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles OutputFileBrowseButton.Click
        SaveFileDialog1.FileName = "Logged GPS Data.txt"
        SaveFileDialog1.DefaultExt = ".txt"
        SaveFileDialog1.Filter = "All Files (*.*)|*.*|Text Files (*.txt)|*.txt"
        SaveFileDialog1.FilterIndex = 1

        ' If they cancelled, abort
        If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.Cancel Then Exit Sub

        ' Remember the path of the output file
        OutputFileTextBox.Text = SaveFileDialog1.FileName

        ' Allow recording now that we have a file name
        StartRecordingButton.Enabled = True
    End Sub

    Private Sub RecordingInterpreter_SentenceReceived(ByVal sender As Object, ByVal e As GeoFramework.Gps.Nmea.NmeaSentenceEventArgs) Handles RecordingInterpreter.SentenceReceived
        ' Add the received sentence to the list box
        ReceivedSentencesListBox.Items.Add(e.Sentence.ToString())
        ReceivedSentencesListBox.SelectedIndex = ReceivedSentencesListBox.Items.Count - 1
    End Sub

    Private Sub StopButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles StopButton.Click
        ' Stop processing GPS data
        RecordingInterpreter.Stop()

        ' Enable Start button
        StartButton.Enabled = True
        PauseButton.Enabled = False
        StopButton.Enabled = False
    End Sub

    Private Sub PauseButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles PauseButton.Click
        ' Is the text "Pause" or "Resume"?
        If PauseButton.Text = "Pause" Then
            PauseButton.Text = "Resume"
            RecordingInterpreter.Pause()
        Else
            PauseButton.Text = "Pause"
            RecordingInterpreter.Resume()
        End If
    End Sub

    Private Sub StartRecordingButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles StartRecordingButton.Click
        ' Start recording all data
        RecordingInterpreter.StartRecording(New FileStream(OutputFileTextBox.Text, FileMode.OpenOrCreate))

        ' Enable Start button
        StartRecordingButton.Enabled = False
        PauseRecordingButton.Enabled = True
        StopRecordingButton.Enabled = True
    End Sub

    Private Sub PauseRecordingButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles PauseRecordingButton.Click
        ' Is the text "Pause Recording" or "Resume Recording"?
        If PauseRecordingButton.Text = "Pause Recording" Then
            PauseRecordingButton.Text = "Resume Recording"
            ' Temporarily suspend recording
            RecordingInterpreter.PauseRecording()
        Else
            PauseRecordingButton.Text = "Pause Recording"
            ' Temporarily suspend recording
            RecordingInterpreter.ResumeRecording()
        End If
    End Sub

    Private Sub StopRecordingButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles StopRecordingButton.Click
        ' Stop recording and close the file
        RecordingInterpreter.StopRecording()

        ' Enable Start button
        StartRecordingButton.Enabled = True
        PauseRecordingButton.Enabled = False
        StopRecordingButton.Enabled = False
    End Sub

    Private Sub RecordingInterpreter_SentenceRecorded(ByVal sender As Object, ByVal e As GeoFramework.Gps.Nmea.NmeaSentenceEventArgs) Handles RecordingInterpreter.SentenceRecorded
        ' Add the recorded sentence to the list box			 
        RecordedSentencesListBox.Items.Add(e.Sentence.ToString())
        RecordedSentencesListBox.SelectedIndex = RecordedSentencesListBox.Items.Count - 1
    End Sub

#End Region

#Region " Playing Back Logged Data "

    Private PlaybackStream As TextFileEmulator

    Private Sub PlaybackInterpreter_AltitudeChanged(ByVal sender As Object, ByVal e As GeoFramework.DistanceEventArgs) Handles PlaybackInterpreter.AltitudeChanged
        ' Set the altimeter to the current altitude
        PlaybackAltimeter.Value = e.Distance
    End Sub

    Private Sub PlaybackInterpreter_BearingChanged(ByVal sender As Object, ByVal e As GeoFramework.AzimuthEventArgs) Handles PlaybackInterpreter.BearingChanged
        ' Set the compass & satellite viewer to the current bearing
        PlaybackCompass.Value = e.Azimuth
        PlaybackSatelliteViewer.Rotation = e.Azimuth
    End Sub

    Private Sub PlaybackInterpreter_SpeedChanged(ByVal sender As Object, ByVal e As GeoFramework.SpeedEventArgs) Handles PlaybackInterpreter.SpeedChanged
        ' Set the speedometer to the current speed
        PlaybackSpeedometer.Value = e.Speed
    End Sub

    Private Sub PlaybackBrowseButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles PlaybackBrowseButton.Click
        OpenFileDialog1.FileName = "Logged GPS Data.txt"
        OpenFileDialog1.DefaultExt = ".txt"
        OpenFileDialog1.Filter = "All Files (*.*)|*.*|Text Files (*.txt)|*.txt"
        OpenFileDialog1.FilterIndex = 1

        ' If they cancelled, abort
        If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.Cancel Then
            Exit Sub
        End If

        ' Remember the path of the output file
        PlaybackLogFileTextBox.Text = OpenFileDialog1.FileName

        ' Allow recording now that we have a file name
        PlaybackPlayButton.Enabled = True
    End Sub

    Private Sub PlaybackPlayButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles PlaybackPlayButton.Click
        Try
            ' Make a stream which will play back the log file
            PlaybackStream = New TextFileEmulator(PlaybackLogFileTextBox.Text)
            ' Assign this stream to the interpreter, which will process the log
            PlaybackInterpreter.BaseStream = PlaybackStream
            ' Start processing data
            PlaybackInterpreter.Start()
            ' Enable the Pause and Stop buttons
            PlaybackPlayButton.Enabled = False
            PlaybackPauseButton.Enabled = True
            PlaybackStopButton.Enabled = True
        Catch ex As Exception
            MessageBox.Show(ex.ToString(), "Unable to Play Back", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
    End Sub

    Private Sub PlaybackSpeed_Scroll(ByVal sender As Object, ByVal e As EventArgs) Handles PlaybackSpeed.Scroll
        ' Change the read interval of the playback stream
        PlaybackStream.ReadInterval = TimeSpan.FromMilliseconds(1001 - PlaybackSpeed.Value)
    End Sub

    Private Sub PlaybackStopButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles PlaybackStopButton.Click
        ' Stop processing recorded data
        PlaybackInterpreter.Stop()

        ' Enable the Pause and Stop buttons
        PlaybackPlayButton.Enabled = True
        PlaybackPauseButton.Enabled = False
        PlaybackStopButton.Enabled = False
    End Sub

    Private Sub PlaybackPauseButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles PlaybackPauseButton.Click
        If PlaybackPauseButton.Text = "Pause" Then
            ' Pause playback
            PlaybackInterpreter.Pause()
            PlaybackPauseButton.Text = "Resume"
        Else
            ' Resume playback
            PlaybackInterpreter.Resume()
            PlaybackPauseButton.Text = "Pause"
        End If
    End Sub

    Private Sub PlaybackInterpreter_SentenceInterpreted(ByVal sender As Object, ByVal e As GeoFramework.Gps.Nmea.NmeaSentenceEventArgs) Handles PlaybackInterpreter.SentenceInterpreted
        ' Add the sentence to the box
        PlaybackSentences.Items.Add(e.Sentence.ToString())
        PlaybackSentences.SelectedIndex = PlaybackSentences.Items.Count - 1
    End Sub

#End Region


End Class